package com.bdqn.mzc.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


/**
 * 邮件工具类
 */

public class Email {
    /**
     * 六位数验证码
     */
    public static String CODE6="";

    /**
     * 163邮箱
     * @param userEail
     * @throws EmailException
     */
    @Deprecated
    public static String sendCodeToEail(String userEail) throws EmailException{
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleEmail email = new SimpleEmail();
        email.setHostName("SMTP.163.com");
        //邮件服务器验证：用户名/密码
        email.setAuthentication("meng1601319217@163.com", "GOFZPNARMVKCGONV");
        // 必须放在前面，否则乱码
        email.setCharset("UTF-8");
        email.addTo(userEail);
        email.setFrom("meng1601319217@163.com", "Gofar");
        email.setSubject("Gofar旅行网验证码信息-" + sdf.format(new Date()));
        StringBuilder msgInfo = new StringBuilder();
        int random = ((int)((Math.random()*9+1)*100000));
        msgInfo.append("验证码为："+random).append("\r\n\t");
        msgInfo.append("此验证码仅作为注册账号使用！").append("\r\n\t");
        msgInfo.append("欢迎加入Gofar！");
        //转换类型
        String rst=String.valueOf(random);
        email.setMsg(msgInfo.toString());
        email.send();
        return rst;
    }


    /**
     * QQ邮箱
     * @param userEail
     * @return
     * @throws MessagingException
     * @throws GeneralSecurityException
     */
    public static String sendCodeToQQEail(String userEail)  throws MessagingException, GeneralSecurityException{
        //创建一个配置文件并保存
        Properties properties = new Properties();
        properties.setProperty("mail.host","smtp.qq.com");
        properties.setProperty("mail.transport.protocol","smtp");
        properties.setProperty("mail.smtp.auth","true");

        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //QQ存在一个特性设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);

        //创建一个session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1601319217@qq.com","oezsghbypffqgaeh");
            }
        });

        //开启debug模式
        session.setDebug(true);
        //获取连接对象
        Transport transport = session.getTransport();
        //连接服务器
        transport.connect("smtp.qq.com","1601319217@qq.com","oezsghbypffqgaeh");
        //创建邮件对象
        MimeMessage mimeMessage = new MimeMessage(session);
        //邮件发送人
        mimeMessage.setFrom(new InternetAddress("1601319217@qq.com"));
        //邮件接收人
        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(userEail));
        //邮件标题
        mimeMessage.setSubject("Gofar旅行网验证码信息-" + sdf.format(new Date()));
        StringBuilder msgInfo = new StringBuilder();
        int random = ((int)((Math.random()*9+1)*100000));
        msgInfo.append("验证码为："+random).append("\r\n\t");
        msgInfo.append("此验证码仅作为注册账号使用！").append("\r\n\t");
        msgInfo.append("欢迎加入Gofar！");
        String rst=String.valueOf(random);
        //邮件内容
        mimeMessage.setContent(msgInfo.toString(),"text/html;charset=UTF-8");
        //发送邮件
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        //关闭连接
        transport.close();
        return rst;
    }

    /**
     * 判断 str是否为邮箱
     * @param str
     * @return
     */
    public static Boolean isEmail(String str) {
        Boolean isEmail = false;
        String expr = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$";

        if (str.matches(expr)) {
            isEmail = true;
        }
        return isEmail;
    }


}
